package edu.ahpu.propertymanage.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import edu.ahpu.propertymanage.dataclass.CostModel;
import edu.ahpu.propertymanage.dataclass.ReturnCostModel;
import edu.ahpu.propertymanage.pojo.*;
import edu.ahpu.propertymanage.service.*;
import edu.ahpu.propertymanage.util.GetID;
import edu.ahpu.propertymanage.util.TimeChange;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/cost")
public class OwnerCostController {

    @Autowired
    OwnerService ownerService;

    @Autowired
    PropertyTageService propertyTageService;

    @Autowired
    GetID getID;

    @Autowired
    EleCostService eleCostService;

    @Autowired
    WaterCostService waterCostService;

    @Autowired
    GasCostServie gasCostServie;

    @Autowired
    PropCostService propCostService;

    @Autowired
    ManageCarService manageCarService;

    @Autowired
    TimeChange timeChange;

    @PostMapping("showwatercost")
    public CostModel showWaterCost(String ownerId) {
        System.out.println(ownerId);
        CostModel costModel = new CostModel();
        Date date = new Date();
        QueryWrapper<WaterCost> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(WaterCost::getOwnerId, ownerId);
        WaterCost waterCost = waterCostService.getOne(wrapper);
        waterCost.setLastMonthWaterTime(timeChange.deleteTime(waterCost.getLastMonthWaterTime()));
        waterCost.setThisMonthWaterTime(timeChange.addAMountTime(waterCost.getLastMonthWaterTime()));

        //获取单价表
//        PropertyTage propertyTage = propertyTageService.getById(waterCost.getWaterPrice());

//        waterCost.setTotalWaterCost(propertyTage.getPrice()*waterCost.getWaterPrice());

        List<Object> list = new ArrayList<>();
        list.add(waterCost);
//        list.add(propertyTage);

        costModel.setList(list);
//        costModel.setObject(waterCost);
        if (date.getTime() > waterCost.getThisMonthWaterTime().getTime())
            costModel.setBool(true);
        else
            costModel.setBool(false);
        return costModel;
    }

    @PostMapping("showelecost")
    public CostModel showEleCost(String ownerId) {
        CostModel costModel = new CostModel();
        Date date = new Date();
        QueryWrapper<EleCost> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(EleCost::getOwnerId, ownerId);
        EleCost eleCost = eleCostService.getOne(wrapper);
        eleCost.setLastMonthEleTime(timeChange.deleteTime(eleCost.getLastMonthEleTime()));
        eleCost.setThisMonthEleTime(timeChange.addAMountTime(eleCost.getLastMonthEleTime()));

        //获取单价表
//        PropertyTage propertyTage = propertyTageService.getById(eleCost.getElePrice());

//        eleCost.setTotalEleCost(propertyTage.getPrice()*eleCost.getUseEleCount());

        List<Object> list = new ArrayList<>();
        list.add(eleCost);
//        list.add(propertyTage);

        costModel.setList(list);
        if (date.getTime() > eleCost.getThisMonthEleTime().getTime())
            costModel.setBool(true);
        else
            costModel.setBool(false);
        return costModel;
    }

    @PostMapping("showgascost")
    public CostModel showGasCost(String ownerId) {
        CostModel costModel = new CostModel();
        Date date = new Date();
        QueryWrapper<GasCost> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(GasCost::getOwnerId, ownerId);
        GasCost gasCost = gasCostServie.getOne(wrapper);
        gasCost.setLastMonthGasTime(timeChange.deleteTime(gasCost.getLastMonthGasTime()));
        gasCost.setThisMonthGasTime(timeChange.addAMountTime(gasCost.getLastMonthGasTime()));

        //获取单价表
//        PropertyTage propertyTage = propertyTageService.getById(gasCost.getGasPrice());

//        gasCost.setTotalGasCost(propertyTage.getPrice()*gasCost.getUseGasCount());

        List<Object> list = new ArrayList<>();
        list.add(gasCost);
//        list.add(propertyTage);

        costModel.setList(list);

        if (date.getTime() > gasCost.getThisMonthGasTime().getTime())
            costModel.setBool(true);
        else
            costModel.setBool(false);
        return costModel;
    }

    @PostMapping("showpropertycost")
    public CostModel showPropertyCost(String ownerId) {
        CostModel costModel = new CostModel();

        Date date = new Date();

        //获取物业费表
        QueryWrapper<PropCost> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(PropCost::getOwnerId, ownerId);
        PropCost propCost = propCostService.getOne(wrapper);
        propCost.setPropBeginDate(timeChange.deleteTime(propCost.getPropBeginDate()));
        propCost.setPropEndDate(timeChange.deleteTime(propCost.getPropEndDate()));


        //获取业主表
        Owner owner = ownerService.getById(ownerId);
        //获取单价表
        PropertyTage propertyTage = propertyTageService.getById(propCost.getPropPrice());

//        propCost.setPropCost(propertyTage.getPrice() * owner.getOwnerHome());
        List<Object> list = new ArrayList<>();
        list.add(owner);
        list.add(propCost);
        list.add(propertyTage);
        costModel.setList(list);
        if (date.getTime() > propCost.getPropEndDate().getTime())
            costModel.setBool(true);
        else
            costModel.setBool(false);
        return costModel;
    }

    @PostMapping("postpropmouth")
    public List postPropMouth(String mouth, @RequestBody PropCost propCost) {
        List list = new ArrayList();
        int m = Integer.valueOf(mouth);

        String ownerId = propCost.getOwnerId();
        Owner owner = ownerService.getById(ownerId);

        QueryWrapper<PropCost> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(PropCost::getOwnerId,ownerId);
        PropCost propCost1 =propCostService.getOne(wrapper);

        PropertyTage propertyTage = propertyTageService.getById(propCost.getPropPrice());
        float cost = propertyTage.getPrice() * owner.getOwnerHome() * m;

        list.add(timeChange.addTime(timeChange.addAnyTime(propCost1.getPropEndDate(), m)));
        list.add(timeChange.addTime(propCost1.getPropEndDate()));
        list.add(cost);

        return list;

    }

    @PostMapping("postpropertycost")
    public boolean postPropertyCost(@RequestBody PropCost propertycost) {
        Date date = new Date();
        System.out.println(propertycost);
        boolean bool = propCostService.saveOrUpdate(propertycost);


        if (date.getTime() > propertycost.getPropEndDate().getTime())
            return true;
        else
            return false;
    }


}
